import threading
import sys
sys.path.append('/data/snmonitor')
# sys.path.append('E:\\snmonitor')
from src.core.controllerBase import msyqlInfo
# from src.core.insertObtainInfo import insertData

# class SelectInterface(object):
#     '''
#     ddd
#     '''
#     def __init__(self):
#         # self.hostname = ''
#         self.nowtime = ''
#         # self.typeline = ''

query_cpu = '''SELECT hostname,cpu_used,cpu_utilization,at_time FROM monitor_cpu WHERE at_time IN (SELECT MAX(at_time) AS at_time FROM monitor_cpu GROUP BY hostname);'''
query_mem = '''SELECT hostname,mem_total,mem_used,mem_utilization,mem_free,at_time FROM monitor_mem WHERE at_time IN (SELECT MAX(at_time) AS at_time FROM monitor_mem GROUP BY hostname);'''
query_disk = '''SELECT hostname,disk_total,disk_used,disk_utilization,disk_free,at_time FROM monitor_disk WHERE at_time IN (SELECT MAX(at_time) AS at_time FROM monitor_disk GROUP BY hostname);'''

def dataHandle(data, keys):
    data = list(data)
    # keys = list(keys)
    for i in data:
        data[data.index(i)] = list(i)
    for a in data:
        if len(a) > 0:
            i = dict(zip(keys, a))
            return i

def selectCpu():
    conn = msyqlInfo()
    cursor = conn.cursor()
    cursor.execute(query_cpu)
    query_c = cursor.fetchall()
    cursor.close()
    conn.close()
    keys = ["hostname", "cpu_used", "cpu_utilization", "at_time"]
    f = dataHandle(query_c, keys)
    return f
    # query_c = list(query_c)
    # for i in query_c:
    #     query_c[query_c.index(i)] = list(i)
    # for a in query_c:
    #     # if len(a) > 0:
    #     i = dict(zip(keys, a))
    #     return i


def selectMem():
    conn = msyqlInfo()
    cursor = conn.cursor()
    cursor.execute(query_mem)
    query_m = cursor.fetchall()
    cursor.close()
    conn.close()

    keys = ["hostname", "mem_total", "mem_used", "mem_utilization", "mem_free", "at_time"]
    f = dataHandle(data=query_m, keys=keys)
    return f
    # query_m = list(query_m)
    # for i in query_m:
    #     query_m[query_m.index(i)] = list(i)
    # for a in query_m:
    #     if len(a) > 0:
    #         i = dict(zip(keys, a))
    #         return i



def selectDisk():
    conn = msyqlInfo()
    cursor = conn.cursor()
    cursor.execute(query_disk)
    query_d = cursor.fetchall()
    cursor.close()
    conn.close()

    keys = ["hostname", "disk_total", "disk_used", "disk_utilization", "disk_free", "at_time"]
    f = dataHandle(query_d, keys)
    return f
    # query_d = list(query_d)
    # for i in query_d:
    #     query_d[query_d.index(i)] = list(i)
    # query_d = zip(keys, query_d)
    # for a in query_d:
    #     if len(a) > 0:
    #         i = dict(zip(keys, a))
    #         return i

if __name__ == '__main__':
    c = selectCpu()
    m = selectMem()
    d = selectDisk()
    print(c)


